const path = require('path')
const fs = require('fs-extra')
const HtmlWebPackPlugin = require('html-webpack-plugin');

/**
* 动态查找所有入口文件
*/
function GetFolderNameArr(path, bool) {
    let components = []
    const files = fs.readdirSync(path)
    //console.log(files, "========files==========");
    files.forEach(function (item, index) {
        let stat = fs.lstatSync(path + item)
        if (stat.isDirectory() === bool) {
            components.push(item)
        }
    })
    return components;
}



const HTML_META = {
    charset: { charset: 'utf-8' },
    'Content-Security-Policy': {
        'http-equiv': 'Content-Security-Policy',
        content:
            "default-src *;style-src 'self' 'unsafe-inline';script-src 'self' 'unsafe-inline' 'unsafe-eval';img-src 'self' data: http: https:;"
    },
    'X-UA-Compatible': { 'http-equiv': 'X-UA-Compatible', content: 'ie=edge' },
    viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no',
    renderer: 'webkit',
    'format-detection': 'telephone=no'
};


// 最原始可用的
function getConfig() {
    let entrys = {}  //设置多页面入口
    let htmlPlugin = []

    var folderNameArr = GetFolderNameArr('./entry/', true)

    folderNameArr.forEach(key => {

        if (key != "meeting") {

            entrys[key] = `./entry/${key}/index.js`

            let plugin = new HtmlWebPackPlugin({
                template: `./entry/${key}/index.html`,
                filename: `${key}.html`,
                chunks: [key],
                meta: HTML_META,
                favicon: './src/imgs/favicon.ico'
            });


            htmlPlugin.push(plugin)
        }


    });


    return { entrys, htmlPlugin }
}


// function getConfig(folderNameArr, notFolderNameArr) {
//     let entrys = {}  //设置多页面入口
//     let htmlPlugin = []

//     var path = './entry/'
//     if (folderNameArr.length > 0) {
//         path = path + folderNameArr.join('/') + '/';
//     }
//     var folderNameArrSearched = GetFolderNameArr(path, true)

//     var preKey = folderNameArr.join("_");

//     console.log(preKey, "===========preKey=============");

//     folderNameArrSearched.forEach(key => {

//         if (notFolderNameArr.indexOf(key) == -1) {

//             entrys[preKey + "_" + key] = `${path}${key}/index.js`

//             if (folderNameArr.length == 0) {
//                 folderNameArr = ['other'];
//             }
//             var filename = "./" + folderNameArr.join('/') + '/' + `${key}.html`;


//             let plugin = new HtmlWebPackPlugin({
//                 template: `${path}${key}/index.html`,
//                 filename: filename,
//                 chunks: [preKey + "_" + key],
//                 meta: HTML_META,
//                 favicon: './src/imgs/favicon.ico'
//             });


//             htmlPlugin.push(plugin)
//         }

//     });



//     return { entrys, htmlPlugin }
// }

// var config1 = getConfig(['meeting'], [])
// var config2 = getConfig([], ['meeting', 'login', 'home', 'demo'])

// var config = {
//     entrys: {
//         ...config1.entrys,
//         ...config2.entrys
//     },
//     htmlPlugin: config1.htmlPlugin.concat(config2.htmlPlugin)
// }

var config = getConfig()

fs.writeFile(path.join(__dirname, '../log/config/util.json'), JSON.stringify(config, null, '\t'), function () { });

module.exports = {
    ...config
}